package com.fbinggun.fbinggunutil.fgive.command.args;

import com.fbinggun.fbinggunutil.FBinggunUtil;
import com.fbinggun.fbinggunutil.command.ACommand;
import com.fbinggun.fbinggunutil.command.MainCommand;
import com.fbinggun.fbinggunutil.fgive.FgiveAPI;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.List;

public class Give extends ACommand {
    FgiveAPI api;
    public Give(MainCommand mainCommand) {
        super(mainCommand);
        api= FBinggunUtil.plugin.getFgiveAPI();
    }

    @Override
    public String getPresentation() {
        return "&e/fgive give <物品名> {数量} {玩家名/*} &7将物品发放给玩家";
    }

    @Override
    public boolean isOp() {
        return true;
    }

    @Override
    public String getPermission() {
        return "";
    }

    @Override
    public String getId() {
        return "give";
    }

    @Override
    public boolean run(CommandSender sender, String[] args) {
        String name= args[1];
        if(name==null || name.isEmpty()){
            sendMessage(sender,"&c请正确使用指令\n"+getPresentation());
            return false;
        }
        if(!api.isExistence(name)){
            sendMessage(sender,"&c物品不存在");
            return false;
        }
        int amount;
        try {
            if(args[2]!=null) {
                amount = Integer.parseInt(args[2]);
            }else {
                amount = 1;
            }
        } catch (NumberFormatException e) {
           sendMessage(sender,"&c请正确使用指令\n"+getPresentation());
            return false;
        }
        String playerName=sender.getName();
        if(args.length>3){
            playerName  =args[3];
        }

        if(playerName==null) {playerName=sender.getName();}

        if(playerName.equalsIgnoreCase("*")){
            for(Player player:Bukkit.getOnlinePlayers()){
                api.give(player,name,amount);
            }
            sendMessage(sender,"&c赐予全体在线玩家 物品: "+name+"*"+amount);
            return false;
        }
       Player targetPlayer= Bukkit.getPlayer(playerName);
        if(targetPlayer==null) {
            sendMessage(sender,"&c玩家未在线");
            return false;
        }
        api.give(targetPlayer,name,amount);
        sendMessage(sender,"&c赐予玩家 物品: "+name+"*"+amount);
        return false;
    }

    @Override
    public boolean isPlayer() {
        return false;
    }

    @Override
    public List<String> getTAB(int l, CommandSender sender, String[] args) {
        List<String> list= super.getTAB(l, sender, args);
        if(args.length==2){
            list.addAll(api.getItemStackList());
            return list;
        }
        if(args.length==4){
            for(Player player:Bukkit.getOnlinePlayers()){
                list.add(player.getName());
            }
            return list;
        }

        if(args.length==3){
            list.add("1");
            list.add("8");
            list.add("16");
            list.add("32");
            list.add("64");
            return list;
        }
        return list;
    }
}
